package com.glsc.ngateway.platform.repo;

import com.glsc.ngateway.platform.domain.Vpn;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VpnRepository extends JpaRepository<Vpn, Integer>, JpaSpecificationExecutor<Vpn> {

    @Modifying
    @Query(value = "truncate table t_gw_vpn",nativeQuery = true)
    void trancateTable();

    @Query(value = "SELECT m FROM Vpn m WHERE 1=1 " +
            "AND (:#{#paramSearchDto.name} IS NULL OR m.name LIKE CONCAT('%', :#{#paramSearchDto.name}, '%')) " +
            "AND (:#{#paramSearchDto.account} IS NULL OR m.account LIKE CONCAT('%', :#{#paramSearchDto.account}, '%')) " +
            "AND (:#{#paramSearchDto.remark} IS NULL OR m.remark LIKE CONCAT('%', :#{#paramSearchDto.remark}, '%')) " +
            "AND (:#{#paramSearchDto.status} IS NULL OR m.status LIKE CONCAT('%', :#{#paramSearchDto.status}, '%')) " +
            "AND (:#{#paramSearchDto.pathName} IS NULL OR m.pathName LIKE CONCAT('%', :#{#paramSearchDto.pathName}, '%')) " +
            "AND (:#{#paramSearchDto.resourceName} IS NULL OR m.resourceName LIKE CONCAT('%', :#{#paramSearchDto.resourceName}, '%')) " +
            "AND (:#{#paramSearchDto.lastLoginTime} IS NULL OR m.lastLoginTime LIKE CONCAT('%', :#{#paramSearchDto.lastLoginTime}, '%')) " +
            "AND (:#{#paramSearchDto.departmentOne} IS NULL OR m.departmentOne LIKE CONCAT('%', :#{#paramSearchDto.departmentOne}, '%')) " +
            "AND (:#{#paramSearchDto.departmentOneId} IS NULL OR m.departmentOneId = :#{#paramSearchDto.departmentOneId}) " +
            "AND (:#{#paramSearchDto.departmentTwo} IS NULL OR m.departmentTwo LIKE CONCAT('%', :#{#paramSearchDto.departmentTwo}, '%')) " +
            "AND (:#{#paramSearchDto.departmentTwoId} IS NULL OR m.departmentTwoId = :#{#paramSearchDto.departmentTwoId}) " +
            "AND (:#{#paramSearchDto.company} IS NULL OR m.company LIKE CONCAT('%', :#{#paramSearchDto.company}, '%')) " +
            "AND (:#{#paramSearchDto.notOa} IS NULL OR m.notOa = :#{#paramSearchDto.notOa}) " +
            "AND (:#{#paramSearchDto.OaStatus} IS NULL OR m.OaStatus LIKE CONCAT('%', :#{#paramSearchDto.OaStatus}, '%'))")
    Page<Vpn> searchByParam(Pageable pageable, Vpn paramSearchDto);

    List<Vpn> findAllByNotOa(Integer notOa);

    List<Vpn> findAllByStatusNot(String status);

    List<Vpn> findAllByStatusIsNull();
}